package cn.hl.hm.quartz;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import cn.hl.hm.dao.DriverCashRecoardMapper;
import cn.hl.hm.dao.VtVlMapper;
import cn.hl.hm.entity.Driver;
import cn.hl.hm.entity.DriverCashRecoard;
import cn.hl.hm.entity.Route;
import cn.hl.hm.entity.RouteVehiclePrice;
import cn.hl.hm.entity.VtVl;
import cn.hl.hm.service.DriverService;
import cn.hl.hm.service.RouteService;
import cn.hl.hm.service.RouteVehiclePriceService;
import cn.hl.hm.service.VehicleTypeService;

@Component
public class RoutePriceQuartz {
	
	@Autowired
	private RouteVehiclePriceService rvpService;
	@Autowired
	RouteService routeService;
	@Autowired
	VehicleTypeService  vehicleTypeService;
	
	@Autowired
	DriverService driverService;
	private final static SimpleDateFormat hhmmSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Autowired
	VtVlMapper vtVlMapper;
	
	
	@Autowired
	DriverCashRecoardMapper dcrMapper;

//	/**
//	 * 添加路线价格
//	 */
//	@Scheduled(cron = "0 00 09 ? * *") //每天9点执行一次
//	public void addRVP() {
//		System.out.println(111);
//		// 获取所有车型
//		List<VehicleType>  vtList = vehicleTypeService.selectVehicleType(null);
//		
//		// 获取所有路线
//		List<Route> routeList = routeService.selectRoute(null);
//		 
//		for (VehicleType vehicleType : vtList) {
//			
//			RouteVehiclePrice rvp = new RouteVehiclePrice();
//			rvp.setVehicleTypeId(vehicleType.getId());
//			
//			for (Route route : routeList) {
//				rvp.setRouteId(route.getId());
//				// 获取某个车型对应某个路线是否存在当日 日期的价格，如果不存在，则新增一条今日的数据
//				List <RouteVehiclePrice> rvpList = rvpService.selectRouteVehiclePriceForAddQuartz(rvp);
//				
//				 // 不存在，新增一条今日的数据
//				if(rvpList == null || rvpList.size()==0){ 
//					List <RouteVehiclePrice> rvpOldList = rvpService.selectRouteVehiclePriceByRouteId(rvp);
//					if(rvpOldList!=null && rvpOldList.size()>0){
//						
//						// 获取昨日最新的一条数据作为今日需要新增的rvpNew对象
//						RouteVehiclePrice rvpNew= rvpOldList.get(0); 
//						rvpNew.setDateTime(hhmmSdf.format(new Date()));
//						rvpNew.setId(null);
//						rvpService.addRouteVehiclePrice(rvpNew);
//					 
//					}
//				}
//					
//					
//				}
//				 
//			}
//		}
	/**
	 * 添加路线价格
	 */
	@Scheduled(cron = "0 00 01 ? * *") //每天9点执行一次 
	public void addRVP() {
		System.out.println(111);
		// 获取所有车型
//		List<VehicleType>  vtList = vehicleTypeService.selectVehicleType(null);
		List<VtVl> vtList = vtVlMapper.selectVtVlList(null);
		
		// 获取所有路线
		List<Route> routeList = routeService.selectRoute(null);
		 
		for (VtVl vv : vtList) {
			
			RouteVehiclePrice rvp = new RouteVehiclePrice();
			rvp.setVtVlId(vv.getId()); 
			
			for (Route route : routeList) {
				rvp.setRouteId(route.getId());
				// 获取某个车型对应某个路线是否存在当日 日期的价格，如果不存在，则新增一条今日的数据
				List <RouteVehiclePrice> rvpList = rvpService.selectRouteVehiclePriceForAddQuartz(rvp);
				
				 // 不存在，新增一条今日的数据
				if(rvpList == null || rvpList.size()==0){ 
					List <RouteVehiclePrice> rvpOldList = rvpService.selectRouteVehiclePriceByRouteId(rvp);
					if(rvpOldList!=null && rvpOldList.size()>0){
						
						// 获取昨日最新的一条数据作为今日需要新增的rvpNew对象
						RouteVehiclePrice rvpNew= rvpOldList.get(0); 
						rvpNew.setDateTime(hhmmSdf.format(new Date()));
						rvpNew.setId(null);
						rvpService.addRouteVehiclePrice(rvpNew);
					 
					}
				}
					
					
				}
				 
			}
		}
		
	/**
	 * 添加路线价格
	 */
	@Scheduled(cron = "0 01 00 ? * *") //每天9点执行一次
	public void addDrvierRemainCashRecoard() {
		
		Driver d = new Driver();
		d.setDriverStatus("3");
		List<Driver> dL = driverService.selectDriver(d);
		DriverCashRecoard dcr = null;
		for (Driver driver : dL) {
			dcr = new DriverCashRecoard();
			dcr.setCreateTime(hhmmSdf.format(new Date()));
			dcr.setDriverAccount(driver.getDriverAccount());
			if(driver.getName()!=null){
				dcr.setDrvierName(driver.getName());
			}
			if(driver.getRemainCash()!=null){
				dcr.setRemainCash(driver.getRemainCash());
			}else{
				dcr.setRemainCash(new BigDecimal(0));
			}
			dcr.setDriverId(driver.getId());
			dcrMapper.insertSelective(dcr);
			
		}
		 
		}
}
